﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Module.DAO.IBL;
using Project.Module.Model;
using Project.Engine.Utilities;

namespace Project.Module.DAO.BL
{
    public class EventBL : IBLTable<Event, int>
    {
        public List<EventModel> GetListEvent() {
            var query = from e in this.Context.Events
                        select new EventModel()
                        {
                            EventID = e.EventID,
                            Description = e.Description,
                            ImageID = e.ImageID != null ? e.ImageID.Value : 0,
                            CreatedDate = e.CreatedDate.HasValue ? e.CreatedDate.Value : Convert.ToDateTime("01/01/1900"),
                            CreatedBy = e.CreatedBy,
                        };

            if (query != null) {
                return query.ToList();
            }

            return null;
        }

        public string GetImagePathById(int imageId) {
            string path = String.Empty;
            var query = from e in this.Context.Images
                        where e.ImageID == imageId
                        select new { ImagePath = e.ImagePath, };
            if (query != null) {
                path = Convert.ToString(query.Select(c => c.ImagePath).FirstOrDefault().ToString());
            }

            return path;
        }

        public EventModel GetDetailEvent(int idEvent)
        {
            var query = from e in this.Context.Events
                        where e.EventID == idEvent
                        select new EventModel()
                        {
                            EventID = e.EventID,
                            Description = e.Description,
                            ImageID = e.ImageID != null ? e.ImageID.Value : 0,
                            CreatedDate = e.CreatedDate.HasValue ? e.CreatedDate.Value : Convert.ToDateTime("01/01/1900"),
                            CreatedBy = e.CreatedBy,
                        };

            if (query != null)
            {
                return query.ToList().FirstOrDefault();
            }

            return null;
        }

        public void InsertComment(string name, string comment, int idEvent)
        {
            int CustomerId = (new CustomerBL()).GetIdByUsername(name);
            if (CustomerId == 0) {
                CustomerId = 1;
            }
            int feedBackId = (new FeedBackBL()).GetMaxId() + 1;
            DateTime date = DateTime.Now;

            string sql = @"insert into Feedback (
                                FeedbackID, CustomerID, Content, 
                                CreatedBy, CreatedDate, EventID
                           ) values (
                                {0}, {1}, {2},
                                {3}, {4}, {5}
                           )";
            var query = this.Context.ExecuteQuery<FeedBackModel>(sql, new object[] { feedBackId, CustomerId, comment, name, date, idEvent }).ToList();
        }

        public void InsertCommentProduct(string name, string comment, int idProduct)
        {
            int CustomerId = (new CustomerBL()).GetIdByUsername(name);
            if (CustomerId == 0)
            {
                CustomerId = 1;
            }
            int feedBackId = (new FeedBackBL()).GetMaxId() + 1;
            DateTime date = DateTime.Now;

            string sql = @"insert into Feedback (
                                FeedbackID, CustomerID, Content, 
                                CreatedBy, CreatedDate, ProductID
                           ) values (
                                {0}, {1}, {2},
                                {3}, {4}, {5}
                           )";
            var query = this.Context.ExecuteQuery<FeedBackModel>(sql, new object[] { feedBackId, CustomerId, comment, name, date, idProduct }).ToList();
        }
    }
}
